<?php
/** 
 * $Source: /cvsroot/pn-commerce/pn-commerce/pntemplates/plugins/modifier.pncShowAsPrice.php,v $
 * $Author: haehnchen $
 * $Revision: 1.14 $
 * $Date: 2005/07/13 09:16:45 $
 *
 * Type: Modifier
 *
 * does numberformatting for decimal values and adds
 * currency symbols if needed
 * @param $amount
 * @param $locale string override monetary settings with this, eg. en_GB@euro
 * @param $decimaldelimiter string override decimal delimiter of the monetary setting 
 * @param $thousandssep string override thousands seperator of the monetary setting 
 * @param $showcurrency bool default=true
 * @param $showthousandssep bool default=true
 * @param $decimals default=2
 * @return string 
 * -------------------------------------------------------------
 */
function smarty_modifier_pncShowAsPrice($amount, $showcurrency = true, $locale = "", $showthousandssep = true, $decimals = 2, $decimaldelimiter = "", $thousandssep = "" )
{
    if(!pnModAPILoad('pncommerce','admin'))
    {
        $smarty->trigger_error( "unable to load pncommerce admin api", E_ERROR );
        return;
    }
    $locsetting = pnModAPIFunc('pncommerce', 'admin', 'getConfigLocale');
    
    // determine which locale to use: parameter or value set in locale config
    $locale = (empty($locale)) ? $locsetting['Locale'] : $locale;
    setlocale(LC_MONETARY, $locale );
	$locale_info = localeconv();
    
    // set thousands separator
    if($showthousandssep === true)
    {
        // order: parameter - manual set value in locale config - locale in locale config
    	$thousandssep = (!empty($thousandssep)) ? $thousandssep : ((!empty($locsetting['ThousandsSeparator'])) ? pnModGetVar('pncommerce','ThousandsSeparator') :$locale_info['mon_thousands_sep'] );
    }
    else
    {
        $thousandssep = "";
    }
    
    // set decimaldelimiter
    $decimaldelimiter = (!empty($decimaldelimiter)) ? $decimaldelimiter : ((!empty($locsetting['DecimalDelimiter'])) ? pnModGetVar('pncommerce','DecimalDelimiter') : $locale_info['mon_decimal_point'] );
	
    $temp = number_format( $amount, $decimals, $decimaldelimiter, $thousandssep);
//		die($locsetting['DefaultCurrencySymbol']);

	if ($showcurrency==true)
	{	
	    switch($locsetting['DefaultCurrencySymbol'])
	    {
	        case 1: $currsymbol =  (!empty($locsetting['CurrencySymbol'])) ? $locsetting['CurrencySymbol'] : $locale_info['currency_symbol'];
	                break;
	        case 2: $currsymbol =  (!empty($locsetting['IntlCurrencySymbol'])) ? $locsetting['IntlCurrencySymbol'] : $locale_info['int_curr_symbol'];
	                break;
	        case 0: 
	        default: $currsymbol = "";
		}
		if( ($locale_info['p_cs_precedes']==127) || ($locale_info['p_sep_by_space']==127)
		  ||($locale_info['n_cs_precedes']==127) || ($locale_info['n_sep_by_space']==127) )
		{
		    // invalid settings - kiss
    	    $temp =  $currsymbol. "&nbsp;" . $temp;
		}
		else
        {
		    if($amount >= 0)
		    {
                $space = ($locale_info['p_sep_by_space']==true) ? "&nbsp;" : "";
		        if($locale_info['p_cs_precedes']==true)
		        {
		            $temp = $currsymbol . $space . $temp;
		        }
		        else
		        {
		            $temp = $temp . $space . $currsymbol;
		        }
		    }
		    else
		    {
                $space = ($locale_info['n_sep_by_space']==true) ? "&nbsp;" : "";
		        if($locale_info['n_cs_precedes']==true)
		        {
		            $temp = $currsymbol . $space . $temp;
		        }
		        else
		        {
		            $temp = $temp . $space . $currsymbol;
		        }
		    }
	    }
	}
	return $temp;
}

?>
